package com.school.base.action.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.school.base.model.Student;
import com.school.base.service.ClassService;
import com.school.base.service.StudentService;
import com.school.common.base.BaseAction;
import com.school.common.constant.Constant;
import com.school.common.util.SpringUtils;
import com.school.base.model.Class;

public class StudentAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6764429624289790027L;
	private static Logger logger = Logger.getLogger(StudentAction.class);
	private static StudentService service = (StudentService) SpringUtils.getBean("base_studentService");
	private Student student;
	private List<Student> studentList;
	private String grade;

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}

	public String list() {
		studentList = service.getStudentList(student, pageBean);
		return SUCCESS;
	}

	public String add() {
		student = new Student();
		student.setGender(Constant.GENDER_MAN);
		return SUCCESS;
	}

	public String addDo() {
		if (student != null) {
			try {
				service.insertStudent(student);
				logger.info("Add student.");
			} catch (Exception e) {
				this.addActionError(e.getMessage());
				return INPUT;
			}
		} else {
			logger.info("Add student fail.");
		}
		return SUCCESS;
	}

	public String edit() {
		if (student != null) {
			if (student.getClassId() > 0) {
				ClassService classService = (ClassService) SpringUtils.getBean("base_classService");
				Class cls = classService.getClassById(student.getClassId());
				grade = cls.getGrade();
			}
			if (student.getId() > 0) {
				Student stu = service.getStudentById(student.getId());
				stu.setClassId(student.getClassId());
				student = stu;
			}
		}

		return SUCCESS;
	}

	public String editDo() {
		if ("POST".equals(this.getHttpServletRequest().getMethod())) {
			if (student != null && student.getId() > 0) {
				try {
					service.updateStudent(student);
					logger.info("Update Student.");
				} catch (Exception e) {
					this.addActionError(e.getMessage());
					return INPUT;
				}
			}
		} else {
			logger.info("Not save student:the request method is not 'post'.");
		}
		return SUCCESS;
	}

	public String delete() {
		if (student != null && student.getId() > 0) {
			service.deleteStudent(student.getId());
			logger.info("Delete student.");
		} else {
			logger.info("Delete student fail:student is null");
		}
		return SUCCESS;
	}

	public void checkLoginName() throws IOException {
		HttpServletResponse response = this.getHttpServletResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		if (student != null && student.getLoginName() != null && !student.getLoginName().isEmpty()) {
			String str = service.checkLoginName(student.getLoginName(), student.getId()) ? "false" : "true";
			response.getWriter().print(str);
		}
		response.getWriter().print("false");
	}

	public List<Class> getClassByGrade(String grade) {
		ClassService classService = (ClassService) SpringUtils.getBean("base_classService");
		List<Class> list = classService.getAllUnexpiredByGrade(grade);
		return list;
	}

	public void getClassByGrade() throws IOException {
		HttpServletResponse response = this.getHttpServletResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		if (grade != null) {
			List<Class> list = this.getClassByGrade(grade);
			if (list != null && list.size() > 0) {
				JSONArray array = new JSONArray();
				for (Class cls : list) {
					JSONObject object = new JSONObject();
					object.put("id", cls.getId());
					object.put("name", cls.getName());
					array.add(object);
				}
				response.getWriter().print(array.toString());
			}
		}
	}
}
